package practise.alogrithm.hundredInterviewQuestions;

import java.util.Stack;

/**
 * Created by richard on 6/1/14.
 */
public class MinFunStack <T extends Comparable> {

    Stack<T> stack = new Stack<>() ;
    Stack<T> minStack = new Stack<>();

    public T min(){
        return minStack.peek();
    }

    public T push(T object){
        stack.push(object);
        T obj = minStack.pop();
        minStack.push(obj.compareTo(object)<0?obj:object);
        return stack.peek();
    }

    public T pop(){
        if(stack.isEmpty()|| minStack.isEmpty()){
            throw new ArrayIndexOutOfBoundsException("Stack is empty");
        }
        minStack.pop();
        return stack.pop();
    }

}
